package com.bana.fvs.app.module.info.page;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.PageBaseParams;
import com.bana.fvs.app.module.info.entity.UpdatePwdBean;
import com.bana.fvs.app.mvp.contract.ChangePhoneContract;
import com.bana.fvs.app.mvp.presenter.ChangePhonePresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.regex.Pattern;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/3/10
 * Time   : 13:40
 * Desc   : 修改手机号
 */
// TODO: 2018/3/11 是否加手机号正则校验？？？？

public class ChangePhoneActivity extends BaseActivity<ChangePhonePresenter> implements ChangePhoneContract.IChangePhoneView {
    private static final String TAG = "dah_ChangePhoneActivity";
    @BindView(R.id.etPhoneNumber)
    EditText mEtPhoneNumber;
    @BindView(R.id.tvConfirm)
    TextView mTvConfirm;
    private String remotePhone;


    public class Params extends PageBaseParams {
        public static final String PARAM_PHONE = "_phone_change_to_";
    }

    @Override
    public int setLayout() {
        return R.layout.activity_change_phone;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setLeftDrawable(-1);
        mBaseTitleBarView.setTitleText(getString(R.string.tip_change_phone));
        mBaseTitleBarView.setLeftTitleGone();
        remotePhone = getIntent().getStringExtra(Params.PARAM_PHONE);
        if (!TextUtils.isEmpty(remotePhone)) {
            mEtPhoneNumber.setText(remotePhone);
            mEtPhoneNumber.setSelection(remotePhone.length());
        }
    }

    @Override
    public void initWidget() {
        mTvConfirm.setOnClickListener(this);
    }


    @Override
    public void widgetClick(View v) {
        if (v.getId() == mTvConfirm.getId()) {
            if (TextUtils.isEmpty(mEtPhoneNumber.getText().toString())) {
                ToastUtil.showShort(mContext, R.string.tip_hint_phone);
                return;
            }
//            if (!isMobile(mEtPhoneNumber.getText().toString())) {
//                ToastUtil.showShort(mContext, R.string.tip_hint_phone_error);
//                return;
//            }
            http_changePhone(mEtPhoneNumber.getText().toString());
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
    }

    @Override
    public void onResponse(UpdatePwdBean updatePwdBean) {
        if (updatePwdBean.getCzjg() == 0) {
            ToastUtil.showShort(mContext, getString(R.string.toast_change_phone_success));
            finish();
        } else {
            ToastUtil.showShort(mContext, "修改手机号码失败");
        }
    }

    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }

    private void http_changePhone(String phone) {
        String token = UserInfoUtils.getInstance().getToken(mContext);
        mPresenter.http_changePhone(mActivity, token, phone);
    }

    private boolean isMobile(String mobile) {
        return Pattern.matches("1\\d{10}", mobile);
    }
}
